package com.feng.cloud.system.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feng.cloud.common.constants.SystemConstant;
import com.feng.cloud.common.exception.system.ParameterException;
import com.feng.cloud.common.exception.system.SystemStatus;
import com.feng.cloud.common.request.QueryRequest;
import com.feng.cloud.common.utils.SortUtils;
import com.feng.cloud.common.utils.WebUtils;
import com.feng.cloud.system.entity.LoginLog;
import com.feng.cloud.system.entity.SystemUser;
import com.feng.cloud.system.mapper.LoginLogMapper;
import com.feng.cloud.system.utils.AddressUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @ClassName loginLogService
 * @Description TODO
 * @Author Better Feng
 * @Date 2019/12/21 21:25
 * @Version 1.0
 **/
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class LoginLogService extends ServiceImpl<LoginLogMapper, LoginLog> {

    public void saveLoginLog(LoginLog loginLog) {
        String username = loginLog.getUsername();
        if (StringUtils.isBlank(username)) {
            throw new ParameterException(SystemStatus.USERNAME_EMPTY);
        }
        loginLog.setLoginTime(LocalDateTime.now());
        String ip = WebUtils.getHttpServletRequestIpAddress();
        loginLog.setIp(ip);
        loginLog.setLocation(AddressUtils.getCityInfo(ip));
        this.save(loginLog);
    }

    /**
     * 获取系统总访问次数
     * @return Long
     */
    public Long findTotalVisitCount() {
        return this.baseMapper.findTotalVisitCount();
    }
    /**
     * 获取今日访问次数
     * @return Long
     */
    public Long findTodayVisitCount() {
        return this.baseMapper.findTodayVisitCount();
    }

    /**
     * 获取今日ip数
     * @return Long
     */
    public Long findTodayIp() {
        return this.baseMapper.findTodayIp();
    }

    /**
     * 获取近十天来的访问记录
     * @param user 用户
     * @return 系统近十天来的访问记录
     */
    public List<Map<String, Object>> findLastTenDaysVisitCount(SystemUser user) {
        return this.baseMapper.findLastTenDaysVisitCount(user);
    }

    public IPage<LoginLog> selectListByPage(QueryRequest request) {
        LambdaQueryWrapper<LoginLog> queryWrapper = new QueryWrapper<LoginLog>().lambda();
        // 用户名
        String username = request.getValue(LoginLog::getUsername,String.class);
        if (StringUtils.isNotBlank(username)) {
            queryWrapper.eq(LoginLog::getUsername, username);
        }
        SortUtils.handleTimeFromTo(request,LoginLog::getLoginTime,queryWrapper);
        Page<LoginLog> page = new Page<>();
        SortUtils.handlePageAndSort(request,page,LoginLog::getLoginTime, SystemConstant.ORDER_DESC);
        return this.baseMapper.selectPage(page, queryWrapper);
    }

    public void deleteLogs(List<String> idList) {
        this.baseMapper.deleteBatchIds(idList);
    }
}
